<script type="text/javascript">
    $(document).ready(function() {
        $("#tabsrapport").tytabs({
            prefixtabs:"tabr",
            prefixcontent:"contentr",
            classcontent:"tabscontent",
            tabinit:"1",
            fadespeed:"normal"
        });
        
        $('.qte3').show();
    });
    
    function toggleSemaine(el) {
        var elId = $(el).attr('id');
        if ($(el).hasClass('plus')) {
            $(el).removeClass('plus');
            $(el).addClass('moins');            
            $('.'+elId).fadeIn(1000);
        } else if ($(el).hasClass('moins')) {
            $(el).removeClass('moins');
            $(el).addClass('plus');
            $('.'+elId).fadeOut(500);
        }
    }
    
    function toggleFamille(el) {
        var elId = $(el).attr('id');
        var wrc = $('.right-content').width()-38;        
        $('.pc-content').css('max-width',wrc);
        
        if ($(el).hasClass('plus')) {
            $(el).removeClass('plus');
            $(el).addClass('moins');            
            $('.'+elId).fadeIn('fast');
        } else if ($(el).hasClass('moins')) {
            $(el).removeClass('moins');
            $(el).addClass('plus');
            $('.'+elId).fadeOut('fast');
        }
    }
</script>

<?php
$x = 1;
$monthTabs = array();
$produitTabs = array();
$monthContents = array();
$monthContentsTop = array();
$monthContentsBottom = array();
$tabFamille = array();
$tmpFamille = array();
$qteMonth = $qteWeek = $qteDay = array();

// Boucle sur les mois
foreach ($period as $idxm => $month) {
    $monthTabs[$idxm] = "";
    $monthContents[$idxm] = "";
    $monthContentsTop[$idxm] = "";
    $monthContentsBottom[$idxm] = "";
    
///////////////////// TOP //////////////////    
    // Recupération des données pour le mois
    foreach($rptProduit as $prd) {
        $explodeOrderDate = explode('-',$prd['orderDate']);
        $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];  

        if($thisMonth == $idxm) {
            @$qteMonth[$idxm][$prd['alibelle']] += $prd['quantite'];
            
            // Récupération de la liste des familles
            if(!isset($tmpFamille[$prd['fname']])) {
                $tabFamille[] = $prd['fname'];
            }
            $tmpFamille[$prd['fname']] = $prd['fname'];
            
            $produitTabs[$prd['alibelle']] = $prd['fname'];
        }
    }
    
    //////////////////////////////////////////////////////////
    $monthTabs[$idxm] .= '<li id="tabr'.$x.'">'.$idxm.'</li>';
    
    $monthContents[$idxm] .= '
        <div id="contentr'.$x.'" class="tabscontent">
            <table style="margin-left:20px">';
    
    $monthContentsTop[$idxm] .= '
        <tr class="tabs-rapport-top">
            <th></th>
            <th>
                <table class="width175">
                    <tr class="haut2">
                        <td colspan="4"><p>'.$idxm.'</p></td>
                    </tr>
                    <tr class="bas">
                        <td width="25%">V</td>
                        <td width="25%">P</td>
                        <td width="25%">O</td>
                        <td width="25%">R</td>
                    </tr>
                </table>
            </th>';
    
    // Boucle sur les semaines
    $y = 1;
    foreach ($month as $idxw => $week) { 
        // Récupération des données pour les semaines
        foreach($rptProduit as $prd) {
            $orderDate = new \DateTime($prd['orderDate']);            
            $orderDateWeek = $orderDate->format('W');

            $explodeOrderDate = explode('-',$prd['orderDate']);
            $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];  

            if($orderDateWeek == $idxw && $thisMonth == $idxm) {
                @$qteWeek[$idxw][$prd['alibelle']] += $prd['quantite'];
            }
        }
        
        //////////////////////////////////////////////////////////
        $monthContentsTop[$idxm] .= '
            <th>
                <table class="width175">
                    <tr class="haut">
                        <td colspan="4">
                            <p>Semaine '.$y.'
                                <span id="rps'.$idxw.'" class="toogle plus" onclick="toggleSemaine(this);"></span>
                            </p>
                        </td>
                    </tr>
                    <tr class="bas">
                        <td width="25%">V</td>
                        <td width="25%">P</td>
                        <td width="25%">O</td>
                        <td width="25%">R</td>
                    </tr>
                </table>
            </th>';

        // Boucle sur les jours
        foreach ($week as $idxd => $day) {
            // Récupération des données pour les jours
            foreach($rptProduit as $prd) {
                if($prd['orderDate'] == $day->format('Y-m-d')) {
                    @$qteDay[$prd['orderDate']][$prd['alibelle']] += $prd['quantite'];
                }
            }
            
            //////////////////////////////////////////////////////////
            $monthContentsTop[$idxm] .= '
                <th class="hide rps'.$idxw.'">
                    <table class="width175">
                        <tr class="haut">
                            <td colspan="4"><p class="blackcolor">'.$idxd.'('.$day->format('d/m/Y').')</p></td>
                        </tr>
                        <tr class="bas">
                            <td width="25%">V</td>
                            <td width="25%">P</td>
                            <td width="25%">O</td>
                            <td width="25%">R</td>
                        </tr>
                    </table>
                </th>';
        }
        $y++;
    }
    
    $monthContentsTop[$idxm] .= '</tr>';
    
    $monthContents[$idxm] .= $monthContentsTop[$idxm];
       
    $x++;
}
foreach ($period as $idxm => $month) {
    foreach($tabFamille as $famille) {
        foreach($produitTabs as $prd => $fml) {
            if($fml == $famille) {
                $_m_qte = isset($qteMonth[$idxm][$prd])?$qteMonth[$idxm][$prd]:0;
                @$fm_qte[$fml][$idxm] += $_m_qte;
                foreach ($month as $idxw => $week) {
                    $_w_qte = isset($qteWeek[$idxw][$prd])?$qteWeek[$idxw][$prd]:0;
                    @$fw_qte[$fml][$idxw] += $_w_qte;

                    foreach ($week as $idxd => $day) {
                        $_d_qte = isset($qteDay[$day->format('Y-m-d')][$prd])?$qteDay[$day->format('Y-m-d')][$prd]:0;
                        @$fd_qte[$fml][$day->format('Y-m-d')] += $_d_qte;
                    }
                }
            }
        }
    }
}

foreach ($period as $idxm => $month) {
    $f = 1;
    foreach($tabFamille as $famille) {
        $monthContentsBottom[$idxm] .= '
            <tr>
                <td id=ft'.$f.' class="ftoggle plus" onclick="toggleFamille(this);">
                    <table style="border-spacing: 0 !important;">
                        <tr>
                            <td width="16px"><span></span></td>
                            <td><nobr>'.  utf8_decode($famille).'</nobr></td>
                        </tr>
                    </table>
                </td>
                <td>
                    <table class="width175">
                        <tr class="detailValue qte3">
                            <td width="25%">'.$fm_qte[$famille][$idxm].'</td>
                            <td width="25%"></td>
                            <td width="25%"></td>
                            <td width="25%"></td>
                        </tr>
                    </table>
                </td>';
        // Boucle sur les semaines
        foreach ($month as $idxw => $week) { 
            $monthContentsBottom[$idxm] .= '<td>
                        <table class="width175">
                            <tr class="detailValue qte3">
                                <td width="25%">'.$fw_qte[$famille][$idxw].'</td>
                                <td width="25%"></td>
                                <td width="25%"></td>
                                <td width="25%"></td>
                            </tr>
                        </table>
                    </td>';
            // Boucle sur les jours
            foreach ($week as $idxd => $day) {
                 $monthContentsBottom[$idxm] .= '<td class="hide rps'.$idxw.'">
                                        <table class="width175">
                                            <tr class="detailValue qte3">
                                                <td width="25%">'.$fd_qte[$famille][$day->format('Y-m-d')].'</td>
                                                <td width="25%"></td>
                                                <td width="25%"></td>
                                                <td width="25%"></td>
                                            </tr>
                                        </table>
                                    </td>';
            }
        }
        $monthContentsBottom[$idxm] .= '</tr>';
        
        foreach($produitTabs as $prd => $fml) {
            if($fml == $famille) {
                $m_qte = isset($qteMonth[$idxm][$prd])?number_format($qteMonth[$idxm][$prd],0,',', ' '):0;
                        
                $monthContentsBottom[$idxm] .= '
                    <tr class="hide ft'.$f.'">
                        <td class="alibelle"><nobr>'.utf8_decode($prd).'</nobr></td>
                        <td>
                            <table class="width175">
                                <tr class="detailValue qte3">
                                    <td width="25%">'.$m_qte.'</td>
                                    <td width="25%"></td>
                                    <td width="25%"></td>
                                    <td width="25%"></td>
                                </tr>
                            </table>
                        </td>';
                
                // Boucle sur les semaines
                foreach ($month as $idxw => $week) { 
                    $w_qte = isset($qteWeek[$idxw][$prd])?number_format($qteWeek[$idxw][$prd],0,',', ' '):0;
                        
                    $monthContentsBottom[$idxm] .= '
                        <td>
                            <table class="width175">
                                <tr class="detailValue qte3">
                                    <td width="25%">'.$w_qte.'</td>
                                    <td width="25%"></td>
                                    <td width="25%"></td>
                                    <td width="25%"></td>
                                </tr>
                            </table>
                        </td>';
                    // Boucle sur les jours
                    foreach ($week as $idxd => $day) {
                        $d_qte = isset($qteDay[$day->format('Y-m-d')][$prd])?number_format($qteDay[$day->format('Y-m-d')][$prd],0,',', ' '):0;
                        
                        $monthContentsBottom[$idxm] .= '
                            <td class="hide rps'.$idxw.'">
                                <table class="width175">
                                    <tr class="detailValue qte3">
                                        <td width="25%">'.$d_qte.'</td>
                                        <td width="25%"></td>
                                        <td width="25%"></td>
                                        <td width="25%"></td>
                                    </tr>
                                </table>
                            </td>';
                    }
                }
                
                $monthContentsBottom[$idxm] .= '</tr>';
            }
        }
        $f++;
    }
    
    $monthContents[$idxm] .= $monthContentsBottom[$idxm];
    
    $monthContents[$idxm] .= '
        </table>
    </div>'; 
}
?>
    
<div id="tabsrapport">
    <ul class="tabs">
        <?php 
        foreach($monthTabs as $li) {
            echo $li;
        } 
        ?>
    </ul>
    <div class="contents">
        <?php 
        foreach($monthContents as $content) {
            echo $content;
        }
        ?>
    </div>
</div>

